package cn.ainit.cw.cwcost;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import cn.ainit.common.ReturnObject;
import cn.ainit.sys.dz.SysDzService;
import cn.ainit.sys.dz.entity.SysDzColumns;
import cn.ainit.sys.dz.entity.SysDzReturnJson;
import cn.ainit.sys.dz.entity.SysDzSource;
import cn.ainit.sys.framework.FrameworkService;
import cn.ainit.sys.menu.SessionCont;
import cn.ainit.sys.query.datastore.DataStore;
import cn.ainit.sys.query.entity.DataGridReceiveParams;
import cn.ainit.sys.query.entity.QueryJson;

/****
 * 查询界面
 * @author xuzq
 *
 */
@Controller
@RequestMapping("/newcwCost")
public class CwCostController {
	
	CwCostService cwcostService;
	@Resource
	public void setCwcostService(CwCostService cwcostService) {
		this.cwcostService = cwcostService;
	}
	
	FrameworkService frameworkService;
	
	@Resource
	public void setFrameworkService(FrameworkService frameworkService) {
		this.frameworkService = frameworkService;
	}
	SysDzService sysDzService;
	
	@Resource
	public void setSysDzService(SysDzService sysDzService) {
		this.sysDzService = sysDzService;
	}
	/**
	 * 成本单生成费用单  （暂时不用了  没有了费用单）
	 * @param costId
	 * @param request
	 * @return
	 * @throws Exception   
	 * @author wyang
	 * @date : 2014-6-3 下午05:18:12
	 */
	@RequestMapping("/payFYExec")
	@ResponseBody
	public ReturnObject payFYExec(String costId,HttpServletRequest request,HttpSession session) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		String keyValue ="";
		try {
			String manId ="";
			Object uid =session.getAttribute(SessionCont.uid);
			if(uid !=null){
				manId = uid.toString();
			}
			String payId = this.cwcostService.newFY(costId,manId);
			if (!"fail".equals(payId)) {
				keyValue = "PAY_ID-"+payId;
				returnObject.setMsg("success");
				returnObject.setInfo(keyValue);
			} 
		} catch (Exception e) {
			keyValue="";
			returnObject.setMsg("fail");
			returnObject.setInfo(keyValue);
			returnObject.setErr(e.getMessage());
		}
		return returnObject;
	}
	
	
	/**
	 * 成本单生成付款单
	 * @param costId
	 * @param request
	 * @return
	 * @throws Exception   
	 * @author wyang
	 * @date : 2014-6-3 下午05:18:12
	 */
	@RequestMapping("/payExec")
	@ResponseBody
	public ReturnObject payExec(String costId,HttpServletRequest request,HttpSession session) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		String keyValue ="";
		try {
			String manId ="";
			Object uid =session.getAttribute(SessionCont.uid);
			if(uid !=null){
				manId = uid.toString();
			}
			returnObject  = this.cwcostService.newPay(costId,manId);
			if ("success".equals(returnObject.getInfo())) {
				keyValue = "PAY_ID-"+returnObject.getMsg();
				returnObject.setMsg(keyValue);
//				returnObject.setInfo("");
			}
		} catch (Exception e) {
			e.printStackTrace();
//			keyValue="";
//			returnObject.setMsg("fail");
//			returnObject.setInfo(keyValue);
//			returnObject.setErr(e.getMessage());
		}
		return returnObject;
	}
	@RequestMapping("/fyExecPay")
	@ResponseBody
	public ReturnObject fyExecPay(String costId,String flag,HttpServletRequest request,HttpSession session) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		String keyValue ="";
		try {
			String manId ="";
			Object uid =session.getAttribute(SessionCont.uid);
			if(uid !=null){
				manId = uid.toString();
			}
			if("1".equals(flag)){
				returnObject  = this.cwcostService.bxfyNewPay(costId, manId);
			}else if("2".equals(flag)){
				returnObject  = this.cwcostService.bxfyNewCZ(costId, manId);
			}else if("3".equals(flag)){
				returnObject = this.cwcostService.bxfyNewJKHK(costId, manId);
			}
			if ("success".equals(returnObject.getInfo())) {
				keyValue = "PAY_ID-"+returnObject.getMsg();
				returnObject.setMsg(keyValue);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
	/**
	 * 根据付款对象和选题名称来选择要付的费用单
	 * @param dsId
	 * @param columnName
	 * @param valueStr
	 * @param manId
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-6-7 下午02:58:52
	 */
	@RequestMapping("/queryFY")
	@ResponseBody
	public QueryJson queryFY(String dsId,String columnName,String valueStr,String manId,HttpServletRequest request) throws Exception{
		QueryJson queryJson = new QueryJson();
		List list = new ArrayList();
		try {
			//1.根据dsId查找 tableName
			String[] dsIds = dsId.split(";");
			DataStore dataStore = frameworkService.getDataStoreById(dsIds[0]);
			String tableName = dataStore.getUpdateTable();
			String sql ="select * from "+tableName+" t where t.XT_ID in (select xt_id from bj_xt where xt_name like '%"+valueStr+"%') and MAN_ID='"+manId+"'";
			//执行sql 返回list<Object>
			list = this.frameworkService.execSql(sql);
			queryJson.setRows(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return queryJson;
	}
	
	/**
	 * 成本单生成预付单
	 * @param costId
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-6-12 上午10:31:45
	 */
	@RequestMapping("/payYF")
	@ResponseBody
	public ReturnObject payYF(String costId,HttpServletRequest request,HttpSession session) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		String keyValue ="";
		try {
			String manId ="";
			Object uid =session.getAttribute(SessionCont.uid);
			if(uid !=null){
				manId = uid.toString();
			}
			returnObject = this.cwcostService.newYF(costId, manId);
			if ("success".equals(returnObject.getInfo())) {
				keyValue = "PAY_ID-"+returnObject.getMsg();
				returnObject.setMsg(keyValue);
//				returnObject.setInfo("");
			}
		} catch (Exception e) {
			e.printStackTrace();
//			keyValue="";
//			returnObject.setMsg("fail");
//			returnObject.setInfo(keyValue);
//			returnObject.setErr(e.getMessage());
		}
		return returnObject;
	}
	/**
	 * 成本单生成还款单
	 * @param costId
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-6-18 下午03:58:24
	 */
	@RequestMapping("/payHK")
	@ResponseBody
	public ReturnObject payHK(String costId,HttpServletRequest request,HttpSession session) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		String keyValue ="";
		try {
			String manId ="";
			Object uid =session.getAttribute(SessionCont.uid);
			if(uid !=null){
				manId = uid.toString();
			}
			returnObject = this.cwcostService.newHK(costId, manId);  /////
			if ("success".equals(returnObject.getInfo())) {
				keyValue = "PAY_ID-"+returnObject.getMsg();
				returnObject.setMsg(keyValue);
//				returnObject.setInfo("");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
	/**
	 * 结算单生成开票单
	 * @param costId
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-6-18 下午03:58:24
	 */
	@RequestMapping("/jsNewKP")
	@ResponseBody
	public ReturnObject jsNewKP(String jsId,HttpServletRequest request,HttpSession session) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		String keyValue ="";
		try {
			String manId ="";
			Object uid =session.getAttribute(SessionCont.uid);
			if(uid !=null){
				manId = uid.toString();
			}
			returnObject = this.cwcostService.jsNewKPFunc(jsId, manId);  /////
			if ("success".equals(returnObject.getInfo())) {
				keyValue = "INV_ID-"+returnObject.getMsg();
				returnObject.setMsg(keyValue);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
	/**
	 * 开票单生成收款单
	 * @param costId
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-6-18 下午03:58:24
	 */
	@RequestMapping("/kpNewSK")
	@ResponseBody
	public ReturnObject kpNewSK(String kpId,HttpServletRequest request,HttpSession session) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		String keyValue ="";
		try {
			String manId ="";
			Object uid =session.getAttribute(SessionCont.uid);
			if(uid !=null){
				manId = uid.toString();
			}
			returnObject = this.cwcostService.kpNewSKFunc(kpId, manId); 
			if ("success".equals(returnObject.getInfo())) {
				keyValue = "CH_ID-"+returnObject.getMsg();
				returnObject.setMsg(keyValue);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
	/**
	 * 付款单中明细中添加成本的按钮
	 * @param bsMan
	 * @param manId
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-6-18 下午04:21:25 
	 */
	@RequestMapping("/queryCB")
	@ResponseBody
	public QueryJson queryCB(String bsMan,String manId,HttpServletRequest request) throws Exception{
		QueryJson queryJson = new QueryJson();
		try {
			String sql = "select * from cw_cost c where c.MAN_BUSINESS ='"+bsMan+"'";
//			if(!"".equals(manId)){
//				sql+=" and nvl(c.MAN_PAY,'NULL') = nvl('"+manId+"','NULL')";
//			}
			List list = this.cwcostService.execSql(sql);
			queryJson.setRows(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return queryJson;
	}
	/*****加载DataList字段*************************************/
	/**
	 * accurate  参数 意思为 查询时 是精确查询还是模糊查询   1为精确查询   0为 模糊查询
	 */
	@RequestMapping("/loadDzDLCard")
	@ResponseBody
	public SysDzReturnJson loadDzDataCard(String dzId,String sourceId,String inputVal,String accurate,HttpServletRequest request) throws Exception{
		SysDzReturnJson queryJson = new SysDzReturnJson();
		try {
			List<SysDzColumns> list = sysDzService.colList(dzId, "");   //datagrid中的列
			SysDzSource dzSource = sysDzService.loadDzSource(dzId);
			queryJson.setColumns(list);
			if (inputVal == null) {
				inputVal = "";
			}
			queryJson.setUrl(request.getContextPath()+"/newcwCost/loadDataCard?dzId="+dzId+"&inputVal="+inputVal+"&accurate="+accurate+"&sourceId="+sourceId);
			queryJson.setReturnColn(dzSource.getReturnCln());
			queryJson.setFunName(dzSource.getFunId());
			queryJson.setShowName(dzSource.getShowName());
			queryJson.setDiyInterFaceFunc(dzSource.getDiyInterFaceFunc());
			return queryJson;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return queryJson;
	}
	
	
	/*******加载数据**************/
	@RequestMapping("/loadDataCard")
	@ResponseBody
	public SysDzReturnJson queryPlanTopic(DataGridReceiveParams dataGParams,String config,String dzId,String sourceId,String inputVal,String accurate,HttpServletRequest request){
		SysDzReturnJson queryJson = new SysDzReturnJson();
		try {
			//String[] configS = config.split(";");
			SysDzSource dzSource = sysDzService.loadDzSource(dzId);
			if (dzSource != null) {
				String getSourceIdSql="(select s.card_no from bs_man s where s.man_id='"+sourceId+"')";
				StringBuffer sqlBuffer = new StringBuffer("select * from ("+dzSource.getSqlText()+") QUERYTMP where 1 = 1 and SIN_ID =  "+getSourceIdSql+"");
				System.out.println("-----------------------------------------------------sqlBuffer:"+sqlBuffer);
//				/---------------------------------
//				if(sourceId!=null && !sourceId.isEmpty() && !"null".equals(sourceId) && !"".equals(sourceId)){
//					sqlBuffer.append(" and SOURCE_ID = '"+sourceId+"'");
//				}
				if(inputVal!=null && !inputVal.isEmpty() && !"null".equals(inputVal) && !"".equals(inputVal)){
					//这里应该用accurate这个参数来判断一下到底是 like 还是 =   
					sqlBuffer.append(" and "+dzSource.getShowName()+" like '%"+inputVal+"%'");
				}
//				/---------------------------------
				SysDzReturnJson tmpQJ = sysDzService.queryRowsBySqlPagination(sqlBuffer.toString(),dataGParams.getPage(),dataGParams.getRows());
				queryJson.setRows(tmpQJ.getRows());
				queryJson.setTotal(tmpQJ.getTotal());
//				queryJson.setUrl(request.getContextPath()+"/framework/loadData?dsId="+dzId+"&config="+config);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return queryJson;
	}
	

	@RequestMapping("/qrtx")
	@ResponseBody
	public boolean qrtx(@RequestBody Map<String,List>  costIds,HttpSession session) throws Exception{
		boolean falg=false;
		try {
			falg=cwcostService.qrtx(costIds.get("costIds"))>0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return falg;
	}
	
}
